a11y: Don't copy attribute names in attribute sets
authorMatthias Clasen <mclasen@redhat.com>
Fri, 19 Mar 2021 21:00:04 +0000 (17:00 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 19 Mar 2021 21:01:28 +0000 (17:01 -0400)
We only need these names when serializing a11y information
for tests. And copying these strings is entirely unnecessary.
So, just pass a callback instead.

gtk/gtkaccessibleattributeset.c
gtk/gtkaccessibleattributesetprivate.h
gtk/gtkatcontext.c

index f49a9cf9112ca2c55cd974363777d588626bab0e..2e7a072abc96ea795df79806bcec01be6c211b3b 100644 (file)
@@ -29,44 +29,41 @@ struct _GtkAccessibleAttributeSet
 {
   gsize n_attributes;
 
+  GtkAccessibleAttributeNameFunc name_func;
   GtkAccessibleAttributeDefaultFunc default_func;
 
   GtkBitmask *attributes_set;
 
-  char **attribute_names;
   GtkAccessibleValue **attribute_values;
 };
 
 static GtkAccessibleAttributeSet *
 gtk_accessible_attribute_set_init (GtkAccessibleAttributeSet          *self,
                                    gsize                               n_attributes,
-                                   const char                        **attribute_names,
+                                   GtkAccessibleAttributeNameFunc      name_func,
                                    GtkAccessibleAttributeDefaultFunc   default_func)
 {
   self->n_attributes = n_attributes;
+  self->name_func = name_func;
   self->default_func = default_func;
-  self->attribute_names = g_new (char *, n_attributes);
   self->attribute_values = g_new (GtkAccessibleValue *, n_attributes);
   self->attributes_set = _gtk_bitmask_new ();
 
   /* Initialize all attribute values, so we can always get the full attribute */
   for (int i = 0; i < self->n_attributes; i++)
-    {
-      self->attribute_names[i] = g_strdup (attribute_names[i]);
-      self->attribute_values[i] = (* self->default_func) (i);
-    }
+    self->attribute_values[i] = (* self->default_func) (i);
 
   return self;
 }
 
 GtkAccessibleAttributeSet *
 gtk_accessible_attribute_set_new (gsize                               n_attributes,
-                                  const char                        **attribute_names,
+                                  GtkAccessibleAttributeNameFunc      name_func,
                                   GtkAccessibleAttributeDefaultFunc   default_func)
 {
   GtkAccessibleAttributeSet *set = g_rc_box_new0 (GtkAccessibleAttributeSet);
 
-  return gtk_accessible_attribute_set_init (set, n_attributes, attribute_names, default_func);
+  return gtk_accessible_attribute_set_init (set, n_attributes, name_func, default_func);
 }
 
 GtkAccessibleAttributeSet *
@@ -84,13 +81,10 @@ gtk_accessible_attribute_set_free (gpointer data)
 
   for (int i = 0; i < self->n_attributes; i++)
     {
-      g_free (self->attribute_names[i]);
-
       if (self->attribute_values[i] != NULL)
         gtk_accessible_value_unref (self->attribute_values[i]);
     }
 
-  g_free (self->attribute_names);
   g_free (self->attribute_values);
 
   _gtk_bitmask_free (self->attributes_set);
@@ -247,7 +241,7 @@ gtk_accessible_attribute_set_print (GtkAccessibleAttributeSet *self,
         continue;
 
       g_string_append (buffer, "    ");
-      g_string_append (buffer, self->attribute_names[i]);
+      g_string_append (buffer, self->name_func (i));
       g_string_append (buffer, ": ");
 
       gtk_accessible_value_print (self->attribute_values[i], buffer);
index bce94d687676ddad4223745a2edea7efcee1751b..cb52b7f62a29634d108d92a858833ccb6b581ad3 100644 (file)
@@ -27,10 +27,11 @@ G_BEGIN_DECLS
 
 typedef struct _GtkAccessibleAttributeSet       GtkAccessibleAttributeSet;
 
+typedef const char *(* GtkAccessibleAttributeNameFunc) (int attribute);
 typedef GtkAccessibleValue *(* GtkAccessibleAttributeDefaultFunc) (int attribute);
 
-GtkAccessibleAttributeSet *     gtk_accessible_attribute_set_new                (gsize                       n_attributes,
-                                                                                 const char                **attr_names,
+GtkAccessibleAttributeSet *     gtk_accessible_attribute_set_new                (gsize                             n_attributes,
+                                                                                 GtkAccessibleAttributeNameFunc    name_func,
                                                                                  GtkAccessibleAttributeDefaultFunc default_func);
 GtkAccessibleAttributeSet *     gtk_accessible_attribute_set_ref                (GtkAccessibleAttributeSet  *self);
 void                            gtk_accessible_attribute_set_unref              (GtkAccessibleAttributeSet  *self);
index 840c64e875e3d7e291ef6812bb1613290c731282..83c9be2a870c80462a3874aa4b61c1ad1f7f3a88 100644 (file)
@@ -384,15 +384,15 @@ gtk_at_context_init (GtkATContext *self)
 
   self->properties =
     gtk_accessible_attribute_set_new (G_N_ELEMENTS (property_attrs),
-                                      property_attrs,
+                                      (GtkAccessibleAttributeNameFunc) gtk_accessible_property_get_attribute_name,
                                       (GtkAccessibleAttributeDefaultFunc) gtk_accessible_value_get_default_for_property);
   self->relations =
     gtk_accessible_attribute_set_new (G_N_ELEMENTS (relation_attrs),
-                                      relation_attrs,
+                                      (GtkAccessibleAttributeNameFunc) gtk_accessible_relation_get_attribute_name,
                                       (GtkAccessibleAttributeDefaultFunc) gtk_accessible_value_get_default_for_relation);
   self->states =
     gtk_accessible_attribute_set_new (G_N_ELEMENTS (state_attrs),
-                                      state_attrs,
+                                      (GtkAccessibleAttributeNameFunc) gtk_accessible_state_get_attribute_name,
                                       (GtkAccessibleAttributeDefaultFunc) gtk_accessible_value_get_default_for_state);
 }